<?php
class ContactController extends Apps_Controller_Action_Default {
	public function indexAction() {
        $rq = $this->_request;
        $f  = new F_Contact();

        $tbVctSetting = new M_DbTable_VctSetting();
        $vctSetting = $tbVctSetting->getVctSetting()->query()->fetch();
        
        if($rq->isPost()) {
            if($f->isValid($rq->getParams())) {
                $fullName = $f->getValue('full_name');
                $email    = $f->getValue('email');
                $phone    = $f->getValue('phone');
                $subject  = $f->getValue('subject');
                $message  = $f->getValue('message');
                $captcha  = $f->getValue('captcha');

                if($email != '' && $fullName != '') {
                    //check captcha
                    $captchaSession = new Zend_Session_Namespace('captchaSession');
                    $captchaCodeSession = $captchaSession->captcha_code;

                    $captchaCode = md5($captcha . '760211');
                    if($captchaCodeSession != $captchaCode) {
                        $this->view->error = "Captcha_doesn't_match";
                    } else {
                        $data = array(
                                'full_name' => $this->quote($fullName),
                                'email'     => $this->quote($email),
                                'phone'     => $this->quote($phone),
                                'subject'   => $this->quote($subject),
                                'message'   => $this->quote($message)
                                );

                        $tbContact = new M_DbTable_Contact();
                        $id = $tbContact->insert($data);
                        if($id) {
                            $this->_flashMessenger->addMessage($vctSetting['reply_message_contact']);
                            $this->goBack();
                        } else {
                            $this->view->error = "Error!";
                        }
                    }
                }
            }
        }
        $this->view->form = $f;

        $tbVctInfo = new M_DbTable_VctInfo();
        $this->view->vctInfo = $tbVctInfo->getVctInfo()->query()->fetch();

        $this->view->vctSetting = $vctSetting;
	}
}